package isanfeng.tuxiaoer.pattern.creational.Esingleton.lazy;

/**
 * 双重检查，保证线程安全。
 * @author 李红辉
 * @version 1.0
 * @date 2019/10/9 11:56
 */
public class LazyDoubleCheckSingleton {
    // 1. 重新将数据载入到缓存
    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;

    private LazyDoubleCheckSingleton() {
    }

    public static LazyDoubleCheckSingleton getInstance() {
        if (lazyDoubleCheckSingleton == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (lazyDoubleCheckSingleton == null) {
                    // 重排序
                    // 1. 分配内存给这个对象
                    // 3. 设置lazyDoubleCheckSingleton指向刚分配的内存地址
                    // 2. 初始化对象
                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }
}
